﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour {

	bool mouseDragging;
	bool fingerScaling;
	Vector3 dragStartPointWorld;
	Vector3 dragCurrentPointWorld;
	Vector3 cameraPositionWorld;
	float speed = 2;
	float height = 13.28f;

	float startLen = 0;

	Camera targetCamera;

	void Start() {
		targetCamera = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<Camera> ();
	}
		
	void Update () {

		//鼠标抓取部分（在target机型上是单指操作）
		if (!mouseDragging && Input.GetMouseButtonDown (0) 
			#if !UNITY_EDITOR
			&& Input.touchCount == 1
			#endif
		) {
			mouseDragging = true;

			Ray ray = targetCamera.ScreenPointToRay (Input.mousePosition);
			RaycastHit hitInfo;
			if (Physics.Raycast (ray, out hitInfo, 1000f, 1 << LayerMask.NameToLayer ("Ground"))) {
				dragStartPointWorld = hitInfo.point;
				Debug.Log ("Dragged");
			}
		}

		if (mouseDragging && Input.GetMouseButton (0) 
			#if !UNITY_EDITOR
			&& Input.touchCount == 1
			#endif
		) {
			//新的drag位置
			Ray ray = targetCamera.ScreenPointToRay (Input.mousePosition);
			RaycastHit hitInfo;

			if (Physics.Raycast (ray, out hitInfo, 1000f, 1 << LayerMask.NameToLayer ("Ground"))) {
				dragCurrentPointWorld = hitInfo.point;

				Debug.Log ("dragCurrentPointWorld" + dragCurrentPointWorld);

				//通过方向得到和相机悬挂面的位置（这里简化，我们的平面一定是水平的，因此，可以直接把（x，z）复制到悬挂面就可以
				//也就是，新的位置x和z是跟ground一致的，y用悬挂面的
				Vector3 dir = (dragStartPointWorld - dragCurrentPointWorld).normalized;
				float len = (dragCurrentPointWorld - dragStartPointWorld).magnitude;

				targetCamera.transform.position += dir * len;
				targetCamera.transform.position = new Vector3 (targetCamera.transform.position.x, height, targetCamera.transform.position.z);		

			}
		}

		if (Input.GetMouseButtonUp (0)) {
			if (mouseDragging)
				mouseDragging = false;
		}


		//鼠标滚轮控制镜头前进后退
		if (Input.GetAxis ("Mouse ScrollWheel") < 0) {
			//zoom out
			Vector3 origin = targetCamera.transform.position;
			Vector3 dir = targetCamera.transform.forward;

			//targetCamera.transform.position = new Vector3 (targetCamera.transform.position.x, targetCamera.transform.position.y + value * 2, targetCamera.transform.position.z);

			targetCamera.transform.position = origin + dir;

			height = targetCamera.transform.position.y;
		}
		else if (Input.GetAxis("Mouse ScrollWheel") > 0) {
			//zoom in
			Vector3 origin = targetCamera.transform.position;
			Vector3 dir = targetCamera.transform.forward;

//			targetCamera.transform.position = new Vector3 (targetCamera.transform.position.x, targetCamera.transform.position.y + value * 2, targetCamera.transform.position.z);

			targetCamera.transform.position = origin - dir;

			height = targetCamera.transform.position.y;
		}


		//触摸控制缩放
		if (!fingerScaling && Input.touchCount == 2) {
			Vector2 pos1 = Input.touches [0].position;
			Vector2 pos2 = Input.touches [1].position;

			startLen = (pos1 - pos2).magnitude;

			fingerScaling = true;

			Debug.Log ("startLen now begin: " + startLen);
		}

		if (fingerScaling && Input.touchCount == 2) {
			Vector2 pos1 = Input.touches [0].position;
			Vector2 pos2 = Input.touches [1].position;

			float currentLen = (pos1 - pos2).magnitude;

			Debug.Log ("currentLen : " + currentLen);
			Debug.Log ("startLen : " + startLen);

			if (currentLen < startLen) {
				//zoom out
				Vector3 origin = targetCamera.transform.position;
				Vector3 dir = targetCamera.transform.forward;

				targetCamera.transform.position = origin - dir;

				height = targetCamera.transform.position.y;
			} else {
				//zoom in
				Vector3 origin = targetCamera.transform.position;
				Vector3 dir = targetCamera.transform.forward;

				targetCamera.transform.position = origin + dir;

				height = targetCamera.transform.position.y;
			}
		}

		if (fingerScaling && Input.touchCount != 2) {
			fingerScaling = false;
			Debug.Log ("fingerScaling stop: " + startLen);
		}
	}
}